package com.tpp.service.impl;

import com.tpp.pojo.Order;
import com.tpp.pojo.TppSeat;
import com.tpp.pojo.baseVo.BaseResult;
import com.tpp.service.SeatService;
import com.tpp.utils.RedisUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import java.util.*;

@Service
public class SeatServiceImpl implements SeatService {
    @Autowired
    RedisUtils redisUtils;
    @Autowired
    RedisTemplate redisTemplate;

    //根据播放厅ID添加座位
    @Override
    public BaseResult addSeat(Map map, String id) {

        Boolean lock = redisTemplate.opsForValue().setIfAbsent("lock", 1);
        if (lock) {
            BaseResult baseResult = new BaseResult();
            List<String> aa = (List<String>) map.get("aa");
            List<String> bb = new ArrayList<>();
            for (String s : aa) {
                boolean b = redisUtils.hHasKey(id, s.split("-")[0]);
                if (b) {
                    String hget = (String) redisUtils.hget(id, s.split("-")[0]);
                    System.out.println(hget);
                    if (hget.contains(s.split("-")[1])) {
                        baseResult.setCode(0);
                        baseResult.setMsg("座位已经被抢走了");
                        return baseResult;
                    } else {
                        bb.add(hget + s.split("-")[1] + ",");
                        boolean hset = redisUtils.hset(id, s.split("-")[0], bb.toString().replace("[", "").replace("]", ""));
                    }
                } else {
                    bb.add(s.split("-")[1].toString() + ",");
                    boolean hset = redisUtils.hset(id, s.split("-")[0], bb.toString().replace("[", "").replace("]", ""));
                }
                bb.clear();
            }
            baseResult.setCode(1);
            baseResult.setMsg("添加座位成功");
            redisTemplate.delete("lock");
            return baseResult;
        }
        return new BaseResult(0,"系统繁忙，请刷新再试");
    }

    //根据影院放映厅场次ID遍历已被占的座位
    @Override
    public BaseResult findAll(String id) {
        System.out.println(id);
        Map<Object, Object> hmget = redisUtils.hmget(id);
        List<TppSeat> objects = new ArrayList<>();
        for (Map.Entry<Object, Object> entry : hmget.entrySet()) {
            String mapKey = entry.getKey().toString().replace("[", "").replace("]", "");
            String mapValue = entry.getValue().toString().replace("[", "").replace("]", "");
            String[] ss = mapValue.split(",");
            System.out.println(mapKey +":" + mapValue);
            for (String s : ss) {
                TppSeat tppSeat = new TppSeat();
                tppSeat.setLineId(Integer.parseInt(mapKey));
                tppSeat.setRowId(Integer.parseInt(s));
                objects.add(tppSeat);
            }
        }
        return new BaseResult(0, "查询成功", objects);
    }

    @Override
    public BaseResult addOrder(Order order) {

        redisUtils.hset("","price",order.getPrice(),900);
        redisUtils.hset("","movie_name",order.getMovie_name(),900);
        redisUtils.hset("","seat",order.getSeat(),900);
        redisUtils.hset("","cinema",order.getCinema(),900);
        redisUtils.hset("","playRoom",order.getPlayRoom(),900);
        redisUtils.hset("","tel",order.getTel(),900);

        return null;
    }

    //通过影院放映厅时间场次的总id，和当前这一次购买的座位数组来删除当前座位
    public BaseResult delete(String id,List<String> list){

        for (String s : list) {
            String hget = (String) redisUtils.hget(id, s.split("-")[0]);
            if (hget.contains(s.split("-")[1])){
                hget.replace(s.split("-")[1],"");
                //将删减过的hget重新存入redis，座位就是被删除后的了
                boolean hset = redisUtils.hset(id, s.split("-")[0], hget);

            }
        }
        return new BaseResult(0,"删除成功");
    }

}